package com.ssy.lingxi.contract.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.contract.entity.ContractPayPlanDO;
import com.ssy.lingxi.contract.model.constant.ContractPayWayEnum;
import com.ssy.lingxi.contract.model.vo.common.response.ContractPayPlanVO;
import com.ssy.lingxi.contract.model.vo.manage.request.ContractPayPlanUpdateVO;
import com.ssy.lingxi.contract.repository.ContractPayPlanRepository;
import com.ssy.lingxi.contract.service.IContractPayPlanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 合同付款计划接口实现
 * @author 伍将
 * @date 2021/2/7
 * @version 2.0.0
 */
@Service
public class ContractPayPlanServiceImpl implements IContractPayPlanService {

    @Resource
    private ContractPayPlanRepository repository;

    /**
     * 根据合同id查询支付计划列表
     * @author 伍将
     * @date 2021/2/7
     * @param contractId: 合同id
     * @return 操作结果
     **/
    @Override
    public Wrapper<List<ContractPayPlanVO>> getPayPlanList(Long contractId) {

        List<ContractPayPlanDO> planDOList = repository.findAllByContractIdOrderByPayNum(contractId);

        List<ContractPayPlanVO> resultList = planDOList.stream().map(r -> {
            ContractPayPlanVO entity = new ContractPayPlanVO();
            entity.setId(r.getId());
            entity.setPayNum(r.getPayNum());
            entity.setPayStage(r.getPayStage());
            entity.setExpectPayTime(DateUtil.format(DateUtil.date(r.getExpectPayTime()), "yyyy-MM-dd"));
            entity.setPayRatio(r.getPayRatio());
            entity.setPayAmount(r.getPayAmount());
            entity.setPayWay(r.getPayWay());
            entity.setPayWayName(ContractPayWayEnum.getMessage(r.getPayWay(), r.getPayParam()));
            entity.setPayParam(r.getPayParam());
            return entity;
        }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }

    /**
     * 保存付款计划
     * @author 伍将
     * @date 2021/2/25
     * @param contractId: 合同id
     * @param payPlanList: 付款计划保存信息
     * @return 操作结果
     **/
    @Transactional
    @Override
    public Wrapper<Void> save(Long contractId, List<ContractPayPlanUpdateVO> payPlanList) {

        // 查询旧数据
        List<ContractPayPlanDO> oldList = repository.findAllByContractIdOrderByPayNum(contractId);

        // 如果存在旧数据，进行匹配删除
        if (oldList.size() > 0) {
            List<ContractPayPlanDO> delList = oldList.stream().filter(o ->
                    !payPlanList.stream().map(ContractPayPlanUpdateVO::getId)
                            .collect(Collectors.toList())
                            .contains(o.getId()))
                    .collect(Collectors.toList());

            // 删除旧数据
            repository.deleteAll(delList);
        }

        // 组装新增/更新数据
        List<ContractPayPlanDO> addList = payPlanList.stream().map(o -> {
            ContractPayPlanDO entity = new ContractPayPlanDO();
            entity.setId(o.getId());
            entity.setContractId(contractId);
            entity.setPayNum(o.getPayNum());
            entity.setPayStage(o.getPayStage());
            entity.setExpectPayTime(DateUtil.parse(o.getExpectPayTime()).getTime());
            entity.setPayRatio(o.getPayRatio());
            entity.setPayAmount(o.getPayAmount());
            entity.setPayWay(o.getPayWay());
            entity.setPayParam(o.getPayParam());
            return entity;
        }).collect(Collectors.toList());
        repository.saveAll(addList);

        return Wrapper.success();
    }

    /**
     * 根据合同id删除付款计划
     * @author 伍将
     * @date 2021/3/3
     * @param contractId: 合同id
     * @return 操作结果
     **/
    @Override
    public Wrapper<Void> deleteByContractId(Long contractId) {
        repository.deleteByContractId(contractId);

        return Wrapper.success();
    }
}
